﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Campus {
    class StealResourceAbility : Ability {
        private int m_moneyToSteal;
        private int m_researchToSteal;
        private int m_chanceOfSuccess;
        private int m_stealTime;
        private int m_elapsedTime;
        private Player m_targetPlayer;
        
        public StealResourceAbility(AbilityMenuIcon icon, BaseEntity owner, int moneyToSeal, int researchToSteal, int percentChanceOfSuccess, int stealTime)
            : base(icon, owner, null, TargetTypes.BaseEntityTarget) {
            m_moneyToSteal = moneyToSeal;
            m_researchToSteal = researchToSteal;
            m_chanceOfSuccess = percentChanceOfSuccess;
            m_stealTime = stealTime;
            MenuDelegate = delegate(string option) {
                Campus.cursor.ActionState = CursorActionState.attack;
                Campus.cursor.ClickMethod = delegate(Vector2 gamePoint) {
                    m_targetPlayer = Cursor.GetBaseEntityAtPoint(gamePoint, Owner.PlayerOwner).PlayerOwner;
                    Owner.SetCurrentAbility(this);
                    Campus.cursor.ClickMethod = null;
                    Campus.cursor.ActionState = CursorActionState.idle;
                };
            };
        }
        public StealResourceAbility(StealResourceAbility copySource)
            : base(copySource) {
            m_moneyToSteal = copySource.m_moneyToSteal;
            m_researchToSteal = copySource.m_researchToSteal;
            m_chanceOfSuccess = copySource.m_chanceOfSuccess;
            m_stealTime = copySource.m_stealTime;
            MenuDelegate = delegate(string option) {
                Campus.cursor.ActionState = CursorActionState.attack;
                Campus.cursor.ClickMethod = delegate(Vector2 gamePoint) {
                    BaseEntity be = Cursor.GetBaseEntityAtPoint(gamePoint, Owner.PlayerOwner);
                    if (be != null) {
                        m_targetPlayer = be.PlayerOwner;
                        Owner.SetCurrentAbility(this);
                    }
                    Campus.cursor.ClickMethod = null;
                    Campus.cursor.ActionState = CursorActionState.idle;
                };
            };
        }
        public override void Update(GameTime gameTime, bool checkInput, bool updateBoundingBox) {
            m_elapsedTime += gameTime.ElapsedRealTime.Milliseconds;
            if (m_stealTime <= m_elapsedTime) {
                if (new Random().Next(0, 100) < m_chanceOfSuccess) {
                    int stolenResearch = Math.Min(m_targetPlayer.Research, m_researchToSteal);
                    int stolenMoney = Math.Min(m_targetPlayer.Money, m_moneyToSteal);

                    m_targetPlayer.AddMoney(-stolenMoney);
                    m_targetPlayer.AddResearch(-stolenResearch);

                    Owner.PlayerOwner.AddMoney(stolenMoney);
                    Owner.PlayerOwner.AddResearch(stolenResearch);
                }
                IsAbilityDone = true;
            }
        }
        public override Ability Clone() {
            return new StealResourceAbility(this);
        }
    }
}
